package com.bw.my.mvvm.viewmodel;

import android.view.View;
import android.view.animation.ScaleAnimation;
import android.widget.Adapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.bw.mvvm.entity.BaseEntity;
import com.bw.mvvm.fleld.UIField;
import com.bw.mvvm.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm.utils.SpUtils;
import com.bw.my.R;
import com.bw.my.mvvm.user.LoginActivity;
import com.bw.my.adapter.ShipAddressRecyclerAdapter;
import com.bw.my.mvvm.model.ShipAddressModel;
import com.bw.my.mvvm.model.entity.ShipAddressEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;


public class ShipAddressViewModel extends BaseViewModel<ShipAddressModel> {

    public String user = SpUtils.getUser();
    int userId = 453;
    public BaseQuickAdapter adapter = new ShipAddressRecyclerAdapter();
    public List<ShipAddressEntity.DataBean> dataBeans = new ArrayList<>();
    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof ShipAddressEntity){
            ShipAddressEntity entity1 = (ShipAddressEntity) entity;
            dataBeans = entity1.getData();
            adapter.setNewInstance(entity1.getData());
            adapter.notifyDataSetChanged();
        }else {
            LogUtils.e(entity.getMessage());
        }
        if (entity.getMessage().equals("添加地址成功")){
            adapter.notifyDataSetChanged();
        }

        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
                if (view.getId() == R.id.item_shipaddress_name){
                    EventBus.getDefault().postSticky(dataBeans.get(position));
                    finishPage();
                }
            }
        });
    }

    public void back(){
        finishPage();
    }
    @Override
    public void onCreate() {
        super.onCreate();
        if(user != null){
            HashMap<String,Object> map = new HashMap<>();
            map.put("id",453);
            RequestBody body = m.createBody(map);
            m.requestAddress(body).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }else {
            HashMap<String,Object> map = new HashMap<>();
            map.put(UIField.PAGECS, LoginActivity.class);
            startPage(map);
        }
    }

    public void newAddress(String name,String addres){
        if(user != null){
            HashMap<String,Object> map = new HashMap<>();
            map.put("shipAddress",addres);
            map.put("shipUserName",name);
            map.put("userId",userId);
            RequestBody body = m.createBody(map);
            m.AddAddress(body).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }else {
            showMsg("请登录");
            HashMap<String,Object> map = new HashMap<>();
            map.put(UIField.PAGECS, LoginActivity.class);
            startPage(map);
        }
    }

}
